<template>
  <div>
    <address-Header></address-Header>
    <section>
      <ul
        v-for="(item, index) in addressList"
        :key="index"
        @click="goAddPath(item)"
      >
        <li>
          <!-- 姓名和电话 -->
          <div>
            <span>{{ item.name }}</span> &nbsp;&nbsp;
            <span>{{ item.tel }}</span>
          </div>
          <!-- 详细地址 -->
          <div>
            <span class="moren" v-if="item.isDefault == '1'">[默认] </span>
            <span>{{ item.province }} </span>&nbsp;
            <span>{{ item.city }} </span>&nbsp;
            <span>{{ item.county }} </span>&nbsp;
            <span>{{ item.addressDetail }} </span>&nbsp;
          </div>
        </li>
      </ul>
      <div class="add-path" @click="goAddPath('add')">添加地址</div>
    </section>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import addressHeader from "./Header.vue";
import Tabbar from "@/components/common/Tabbar.vue";
import qs from "qs";

export default {
  name: "Address",
  components: {
    addressHeader,
    Tabbar,
  },
  data() {
    return {
      token: {
        token: "",
      },
      addressList: [],
    };
  },
  created() {
    this.getAddressList();
  },
  methods: {
    //跳转到添加地址页面
    goAddPath(key) {
      this.$router.push({
        name: "AddressList",
        params: {
          key: JSON.stringify(key),
        },
      });
      // console.log(key);
    },
    //显示用户地址的数据
    getAddressList() {
      this.token.token = window.sessionStorage.getItem("token");
      this.$http.post("/getAddress", qs.stringify(this.token)).then((res) => {
        // console.log(res.data);
        this.addressList = res.data;
      });
    },
  },
};
</script>

<style lang="less" scoped>
section {
  display: flex;
  align-items: center;
  flex-direction: column;
  height: 100vh;
  background-color: #f7f7f7;
  ul {
    width: 100%;
    li {
      margin: 6px 0;
      padding: 10px 9px;
      background-color: #fff;
      font-size: 14px;
      .moren {
        color: #b0352f;
      }
    }
  }

  .add-path {
    background-color: #b0352f;
    margin-top: 20px;
    border-radius: 5px;
    width: 90px;
    height: 34px;
    line-height: 34px;
    text-align: center;
    font-size: 15px;
    color: #fff;
  }
}
</style>